package com.newsystem.controller;


import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.newsystem.MyUtils.CommonResult;
import com.newsystem.mapper.GeneralUserMapper;
import com.newsystem.mapper.UserMapper;
import com.newsystem.pojo.GeneralUser;
import com.newsystem.pojo.User;
import com.newsystem.pojo.vo.GeneralUserInfo2;
import com.newsystem.service.impl.GeneralUserService;
import io.swagger.annotations.ApiOperation;
import lombok.RequiredArgsConstructor;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestMapping;

import org.springframework.web.bind.annotation.RestController;

/**
 * <p>
 *  普通用户
 * </p>
 *
 * @author chenlitao
 * @since 2022-11-06
 */
@RestController
@RequestMapping("/general")
@RequiredArgsConstructor
public class GeneralUserController {

    private final GeneralUserMapper generalUserMapper;
    private final GeneralUserService generalUserService;
    private final UserMapper userMapper;

    @ApiOperation("普通用户注册")
    @PutMapping("/register")
    public CommonResult<Object> register(User user, GeneralUser generalUser){
        if(user.getAccount() == null){
            return CommonResult.failed("用户账号不能为空");
        }
        if(user.getPassword() == null){
            return CommonResult.failed("用户密码不能为空");
        }
        if(user.getRole()==1){
            return generalUserService.insertOneGeneralUser(user,generalUser);
        }
        else {
            return CommonResult.failed("请确定是否申请一个普通账号");
        }
    }

    //获取自己的个人信息
    @ApiOperation("普通用户获取自己的个人信息")
    @GetMapping("/getinfo")
    public CommonResult<Object> getInfo(String generalUserId){
        return generalUserService.selectGeneralUsrInfoById(generalUserId);
    }



    //修改自己个人信息
    @ApiOperation("修改自己个人信息")
    @PutMapping("/update")
    public CommonResult<Object> update(GeneralUserInfo2 generalUser){
        if(generalUserMapper.updateGeneralUser(generalUser) >0) {
            return CommonResult.success("更新成功");
        }
        return CommonResult.failed("用户信息更新失败");
    }


}

